package com.project.datacenter.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.datacenter.dto.ParamDto;
import com.project.datacenter.entity.BusLine;
import com.project.datacenter.entity.TobjBusDirection;
import com.project.datacenter.entity.TobjBusLine;
import com.project.datacenter.entity.TobjBusVehicle;
import com.project.datacenter.mapper.BusLineMapper;
import com.project.datacenter.mapper.TobjBusLineMapper;
import com.project.datacenter.mapper.UserXlMapper;
import com.project.datacenter.service.BusLineService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BusLineServiceImpl implements BusLineService {

    @Resource
    private BusLineMapper busLineMapper;
    @Resource
    private TobjBusLineMapper tobjBusLineMapper;
    @Resource
    private UserXlMapper userXlMapper;

    @Override
    @DS("datacenter")
    public List<TobjBusLine> getList() {
        return tobjBusLineMapper.getList();
    }

    @Override
    @DS("master")
    public List<TobjBusLine> getListByDate(ParamDto paramDto) {
        return tobjBusLineMapper.selectByDate(paramDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void saveList(List<TobjBusLine> list) {
        for (TobjBusLine entity : list) {
            tobjBusLineMapper.insertSelective(entity);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteByDate(ParamDto paramDto) {
        tobjBusLineMapper.deleteByDate(paramDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteExpireDate(ParamDto paramDto) {
        tobjBusLineMapper.deleteExpireDate(paramDto);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public List<BusLine> update(List<TobjBusLine> list, List<TobjBusDirection> directionList, List<TobjBusVehicle> vehicleList) {
        List<BusLine> newBusLineResults = new ArrayList<>();
        List<BusLine> busLineList = busLineMapper.selectAll();
        for (TobjBusLine tobjBusLine : list) {
            if (busLineList.size() > 0) {
                List<BusLine> busLineList1 = busLineList.stream().filter(item -> item.getYgxlid().equals(tobjBusLine.getBusline_id())).collect(Collectors.toList());
                if (busLineList1.size() > 0) {
                    if ("2".equals(tobjBusLine.getDispatch()) || "7".equals(tobjBusLine.getDispatch())) {
                        busLineList1.get(0).setXlmc(tobjBusLine.getFullname() + "区间");
                    } else if ("3".equals(tobjBusLine.getDispatch())) {
                        busLineList1.get(0).setXlmc(tobjBusLine.getFullname() + "B线");
                    } else if ("4".equals(tobjBusLine.getDispatch())) {
                        busLineList1.get(0).setXlmc(tobjBusLine.getFullname() + "大站");
                    } else if ("5".equals(tobjBusLine.getDispatch())) {
                        busLineList1.get(0).setXlmc(tobjBusLine.getFullname() + "定班");
                    } else if ("6".equals(tobjBusLine.getDispatch())) {
                        busLineList1.get(0).setXlmc(tobjBusLine.getFullname() + "跨线");
                    } else {
                        busLineList1.get(0).setXlmc(tobjBusLine.getFullname());
                    }
                    busLineList1.get(0).setYgxlmc(tobjBusLine.getFullname());
                    busLineList1.get(0).setXlmc(tobjBusLine.getFullname());
                    busLineList1.get(0).setYhid(tobjBusLine.getEnterprise_id());
                    busLineList1.get(0).setZygxlid(tobjBusLine.getMainline_id());
                    if (!StringUtils.isEmpty(tobjBusLine.getDispatch())) {
                        busLineList1.get(0).setDdfs(Integer.parseInt(tobjBusLine.getDispatch()));
                    }
                    busLineList1.get(0).setXltdlx(tobjBusLine.getDispatch());
                    if (!StringUtils.isEmpty(tobjBusLine.getLinecode())) {
                        busLineList1.get(0).setXldm(Integer.parseInt(tobjBusLine.getLinecode()));
                    }
                    busLineList1.get(0).setXlqs(tobjBusLine.getLinebelong());
                    if (!StringUtils.isEmpty(tobjBusLine.getOperationtype())) {
                        busLineList1.get(0).setYysj(Integer.parseInt(tobjBusLine.getOperationtype()));
                    }
                    if (tobjBusLine.getLength() != null) {
                        busLineList1.get(0).setXlcd(String.valueOf(tobjBusLine.getLength()));
                    } else {
                        busLineList1.get(0).setXlcd("0");
                    }
                    // 线路走向处理
                    if (directionList.size() > 0) {
                        List<TobjBusDirection> directions0 = directionList.stream().filter(item -> item.getBusline_id().equals(busLineList1.get(0).getYgxlid()) && "1".equals(item.getDirectiontype())).collect(Collectors.toList());
                        if (directions0.size() > 0) {
                            String result0 = directions0.stream().map(item -> item.getRoadname()).collect(Collectors.joining(","));
                            busLineList1.get(0).setXlsxzx(result0);
                        }
                        List<TobjBusDirection> directions1 = directionList.stream().filter(item -> item.getBusline_id().equals(busLineList1.get(0).getYgxlid()) && "2".equals(item.getDirectiontype())).collect(Collectors.toList());
                        if (directions1.size() > 0) {
                            String result1 = directions1.stream().map(item -> item.getRoadname()).collect(Collectors.joining(","));
                            busLineList1.get(0).setXlxxzx(result1);
                        }
                    }
                    // 配车数处理
                    int count = 0;
                    if (vehicleList.size() > 0) {
                        List<TobjBusVehicle> vehicles0 = vehicleList.stream().filter(item -> item.getBusline_id().equals(busLineList1.get(0).getYgxlid())).collect(Collectors.toList());
                        if (vehicles0.size() > 0) {
                            for (TobjBusVehicle tobjBusVehicle : vehicles0) {
                                count = count + Integer.parseInt(tobjBusVehicle.getVehicleamount());
                            }
                        }
                        busLineList1.get(0).setJhpcs(count);
                    }
                    busLineList1.get(0).setState(0);
                    busLineMapper.updateById(busLineList1.get(0));
                } else {
                    BusLine busLine = new BusLine();
                    if ("2".equals(tobjBusLine.getDispatch()) || "7".equals(tobjBusLine.getDispatch())) {
                        busLine.setXlmc(tobjBusLine.getFullname() + "区间");
                    } else if ("3".equals(tobjBusLine.getDispatch())) {
                        busLine.setXlmc(tobjBusLine.getFullname() + "B线");
                    } else if ("4".equals(tobjBusLine.getDispatch())) {
                        busLine.setXlmc(tobjBusLine.getFullname() + "大站");
                    } else if ("5".equals(tobjBusLine.getDispatch())) {
                        busLine.setXlmc(tobjBusLine.getFullname() + "定班");
                    } else if ("6".equals(tobjBusLine.getDispatch())) {
                        busLine.setXlmc(tobjBusLine.getFullname() + "跨线");
                    } else {
                        busLine.setXlmc(tobjBusLine.getFullname());
                    }
                    busLine.setYgxlmc(tobjBusLine.getFullname());
                    busLine.setYhid(tobjBusLine.getEnterprise_id());
                    busLine.setZygxlid(tobjBusLine.getMainline_id());
                    if (!StringUtils.isEmpty(tobjBusLine.getDispatch())) {
                        busLine.setDdfs(Integer.parseInt(tobjBusLine.getDispatch()));
                    }
                    busLine.setXltdlx(tobjBusLine.getDispatch());
                    if (!StringUtils.isEmpty(tobjBusLine.getLinecode())) {
                        busLine.setXldm(Integer.parseInt(tobjBusLine.getLinecode()));
                    }
                    busLine.setXlqs(tobjBusLine.getLinebelong());
                    if (!StringUtils.isEmpty(tobjBusLine.getOperationtype())) {
                        busLine.setYysj(Integer.parseInt(tobjBusLine.getOperationtype()));
                    }
                    if (tobjBusLine.getLength() != null) {
                        busLine.setXlcd(String.valueOf(tobjBusLine.getLength()));
                    } else {
                        busLine.setXlcd("0");
                    }
                    // 线路走向处理
                    if (directionList.size() > 0) {
                        List<TobjBusDirection> directions0 = directionList.stream().filter(item -> item.getBusline_id().equals(tobjBusLine.getBusline_id()) && "1".equals(item.getDirectiontype())).collect(Collectors.toList());
                        if (directions0.size() > 0) {
                            String result0 = directions0.stream().map(item -> item.getRoadname()).collect(Collectors.joining(","));
                            busLine.setXlsxzx(result0);
                        }
                        List<TobjBusDirection> directions1 = directionList.stream().filter(item -> item.getBusline_id().equals(tobjBusLine.getBusline_id()) && "2".equals(item.getDirectiontype())).collect(Collectors.toList());
                        if (directions1.size() > 0) {
                            String result1 = directions1.stream().map(item -> item.getRoadname()).collect(Collectors.joining(","));
                            busLine.setXlxxzx(result1);
                        }
                    }
                    // 配车数处理
                    int count = 0;
                    if (vehicleList.size() > 0) {
                        List<TobjBusVehicle> vehicles0 = vehicleList.stream().filter(item -> item.getBusline_id().equals(tobjBusLine.getBusline_id())).collect(Collectors.toList());
                        if (vehicles0.size() > 0) {
                            for (TobjBusVehicle tobjBusVehicle : vehicles0) {
                                count = count + Integer.parseInt(tobjBusVehicle.getVehicleamount());
                            }
                        }
                        busLine.setJhpcs(count);
                    }
                    busLine.setYgxlid(tobjBusLine.getBusline_id());
                    busLine.setState(0);
                    busLineMapper.insertSelective(busLine);
                    List<BusLine> insertList = busLineMapper.selectByYgxlid(busLine);
                    if (insertList.size() > 0) {
                        newBusLineResults.add(insertList.get(0));
                    }
                }
            }
        }
        // 更新废弃线路
        for (BusLine busLineDelete : busLineList) {
            List<TobjBusLine> deletes = list.stream().filter(item -> item.getBusline_id().equals(busLineDelete.getYgxlid())).collect(Collectors.toList());
            if (deletes.size() == 0) {
                busLineMapper.deleteById(busLineDelete);
                busLineDelete.setState(1);
                newBusLineResults.add(busLineDelete);
            }
        }
        return newBusLineResults;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void updateZjLine() {
        // 更新子线路
        List<BusLine> zjList = busLineMapper.selectZjgxlid();
        for (BusLine zj : zjList) {
            busLineMapper.updateByZjgxlid(zj);
        }
        // 更新线路所属规则
        busLineMapper.updateRuleId();
        List<BusLine> zjRuleIdList = busLineMapper.selectZjruleId();
        for (BusLine zj : zjRuleIdList) {
            busLineMapper.updateZjRuleId(zj);
        }
    }
}
